/**
 * Created by EEO on 2017/7/1.
 */
require(['init'], function () {
    var $pagination = $('#js-pagination')
    var pagination;
    var doctorId;
    var teacheruid;
    var classuid = location.search.urlToObj().classuid;
    var classname = location.search.urlToObj().name;
    $('.classname').text(classname);
    $('#J_classlist').on('click', '.remake', function (e) {
        var studentuid = $(e.target).data("id");
        var studentname = $(e.target).data("name");
        ajaxMethod({
            url: '/api/remark/student/' + studentuid,
            success: function (data) {
                var $formList = $('<div class="formList" style="border: 1px solid #d8d8d8;display:block;overflow-y: scroll;height: 255px;margin-top:10px;"></div>');
                $.each(data, function (i, t) {
                    var $formLi = $(' <div class="formLi">\
                        <div class="formTitle">\
                        <span class="titleText">\
                            <span class="icon icon-size-16 ">\
                            </span>' + (i + 1) + '\
                        </span>\
                        </div>\
                        <div class="border-top" style="border-top: 1px solid #d8d8d8;width: 95%;">\
                        <div class="formTitle">\
                        <span>' + t.teachername + '</span><br>\
                        <span>' + t.created_at.split(' ')[0] + '</span><br>\
                        <span>' + t.classname + '</span>\
                        </div>\
                        <div class="formContent">' + t.remark + '\
                        </div>\
                        </div>\
                    </div>')
                    $formList.append($formLi)
                });
                $formList.find('.border-top').find('.formTitle').css('text-align', 'left');
                $formList.find('.border-top').first().css('border', 'none');
                var popUpBox = new PopUpBox({
                    type: 'dom',
                    style: 'default',
                    submitBtnType: 'confirm',
                    //titleText: '备注' + ' ( ' + data.length + '条 )',
                    titleText: i18next.t('popupbox.remark') + ' ( ' + data.length + ''+i18next.t('popupbox.item')+ ')',
                    show: true,
                    content: $formList,
                    callback: {
                        yes: function() {
                            var rem = $remark.find('[name=remark]').val();
                            rem = rem.replace(/^\s+|\s+$/g, '');
                            if (rem.length == 0)return;
                            ajaxMethod({
                                url: ' /api/user/current',
                                type: 'get',
                                success: function (data) {
                                    doctorId = data.userid;
                                    ajaxMethod({
                                        url: ' /api/teacher/user/' + doctorId,
                                        type: 'get',
                                        success: function (data) {
                                            teacheruid = data.teacheruid;
                                            ajaxMethod({
                                                url: '/api/remark/student',
                                                type: 'post',
                                                data: {
                                                    classeid: classuid,
                                                    teacheruid: teacheruid,
                                                    studentid: studentuid,
                                                    teachername: data.lastname,
                                                    classname: classname,
                                                    remark: rem
                                                },
                                                success: function (data) {
                                                    window.location.reload();
                                                }
                                            });
                                            //老师 id
                                        }
                                    })
                                }
                            })
                        }
                    },
                    position: {
                        width: 600,
                        height: 515
                    }
                });
                if (data.length == 0) {
                    $('.formList').css('border', 'none');
                    $('.formList').css('text-align', 'center');
                    $('.formList').html(i18next.t('popupbox.thestudentsnoremark'));
                }
                var $remark = $('<div style="margin-top: 10px"><div class="title"><span>'+i18next.t('popupbox.addremark')+':</span></div><div class="content"><textarea  rows="4" cols="77" style="margin-top: 10px;border: 1px solid #ccc;border-radius: 5px;color:#818e9b;padding-left:15px;" name="remark" value=""></textarea></div></div>');
                //var name = $('<div> ' + studentname + '</div>');
                var name = $('<div>'+i18next.t('table.studentname')+' : ' + studentname + '</div>');
                $('.popUpBoxContent').append($remark);
                $('.formList').wrap(name);
                $('.popUpBoxContent').css('padding', '15px');
            }

        });
    });
    function getClassList(query, cb) {
        ajaxMethod({
            url: '/api/teacher/class/' + classuid,
            data: query || {},
            success: function (data) {
                console.log(data)
                cb && cb(data)
                if(data.list.length == 0) {
                    $('#J_classlist').html('<div style="margin-top: 50px;text-align: center;">'+i18next.t('input.nullresults')+'</div>');
                    $pagination.hide()
                    return;
                }
                var $classlistTable = $('#J_classlist').html('');
                var $thead = $('<thead>\
          <tr class="enroll">\
            <th>'+i18next.t('table.no')+'</th>\
            <th>'+i18next.t('table.studentname')+'</th>\
            <th>'+i18next.t('table.thelatesttimeforclass')+'</th>\
            <th>'+i18next.t('table.grade')+'</th>\
            <th>'+i18next.t('table.birth')+'</th>\
            <th>'+i18next.t('table.city')+'</th>\
            <th>'+i18next.t('table.studentinfo')+'</th>\
            <th>'+i18next.t('table.remark')+'</th>\
          </tr>\
        </thead>').appendTo($classlistTable);
                var $tbody = $('<tbody class="student_list"></tbody>').appendTo($classlistTable);
                $.each(data.list, function (index, item) {
                    console.log(item)
                    var $classItem = $('<tr class=""></tr>');
                    $classItem.append('<td>' + (index + 1) + '</td>');
                    $classItem.append('<td>' + item.name + '</td>');
                    $classItem.append('<td>' + (item.last == null ? " " : (new Date(item.last).Format('YYYY-MM-DD'))) + '</td>');
                    $classItem.append('<td>' + (item.grade == null ? " " : item.grade) + '</td>');
                    $classItem.append('<td>' + (item.birthday == null ? " " : (new Date(item.birthday).Format('YYYY-MM-DD'))) + '</td>');
                    $classItem.append('<td>' + (item.city == null ? " " : item.city) + '</td>');
                    //  $classItem.append('<td>' + (new Date(item.begindate).Format('YYYY-MM-DD') || '--') + '</td>');
                    var $infoTd = $('<td></td>').appendTo($classItem);
                    var $remarkTd = $('<td></td>').appendTo($classItem);
                    var $info = $('<a href="student_details.html?studentuid=' + item.studentuid + '&name=' + $('.classname').text() + '&id=' + classuid + '"class=" btn btn-primary btn-sm btn-inverse ml5"><span style="color:#398dee">'+i18next.t('table.details')+'</span></a>').appendTo($infoTd);
                    var $remark = $('<span style="color:#398dee" class="btn btn-primary btn-sm btn-inverse ml5 remake" data-name="' + item.name + '" data-id=' + item.studentuid + '>查看</span></a>').appendTo($remarkTd);
                    if (item.hasRemark) {
                        $remark.text(''+i18next.t('table.look')+'')
                    } else {
                        $remark.text(''+i18next.t('table.addremark')+'')
                    }
                    $tbody.append($classItem);
                    //排序
                    var len = $('table tr').length;
                    for (var i = 1; i < len; i++) {
                        $('table tr:eq(' + i + ') td:first').text(i);
                    }
                    //把所有null的字段都换成---
                    $tbody.find("tr").each(function () {
                        //console.log(this)
                        var tdhtml = $(this).children();
                        $.each(tdhtml, function (i, t) {
                            //console.log($(t).text());
                            //console.log(t)
                            $(t).text() == 'null' ? $(t).text('--') : $(t).text()
                            $(t).text() == '0000-00-00' ? $(t).text('--') : $(t).text()
                            $(t).text() == 'NaNaN-aN-aN' ? $(t).text('--') : $(t).text()
                            $(t).text() == '' ? $(t).text('--') : $(t).text()
                        })
                    });
                });
                $pagination.show()
            }
        })
    }



//搜索
    function loadSearchData(querystr) {
        if(querystr) {
            pagination.filter.name = querystr;
            pagination.filter.mobile = querystr;
            pagination.filter.ClassInNo = querystr;
            pagination.searchData()
        } else {
            pagination.reloadData()
        }
    }
    $('#J_teacher_search').on('click', function () {
        var $input = $(this).prev();
        var querystr = $.trim($input.val());
        loadSearchData(querystr)
    });
    $('.searchInput').on('keyup', function (e) {
        if (e.keyCode == 13) {
            if (e.keyCode == 13) {
                var querystr = $.trim($(this).val());
                loadSearchData(querystr)
            }
        }
    });
    pagination = new Pagination($pagination, getClassList)
});
